package com.ls.fw.config.zookeeper;

import java.util.List;
import java.util.Map;

import com.ls.fw.config.zookeeper.listener.ZookeeperNodeListener;
import com.ls.fw.config.zookeeper.op.ZookeeperTransaction;

/**
 * 
 * @author ls
 */
public interface ZookeeperClient extends Cloneable {

	String CONNECTION_STRING_JVM_PROPERTY_NAME = "zookeeper.connectionString";
	String DEFAULT_CONNECTION_STRING = "127.0.0.1:" + 2181;

	// ****************************** //

	boolean nodeExists(String path);

	/**
	 * Creates a persistent Zookeeper node with an empty data. Causes any parent
	 * nodes to get created if they haven't already been.
	 */
	void createNode(String path);

	/**
	 * Creates a persistent Zookeeper node with the data {@code data}. Causes
	 * any parent nodes to get created if they haven't already been.
	 */
	void createNode(String path, String data);

	void ensureNode(String path);

	void updateNodeData(String path, String data);

	void deleteNode(String path);

	String getString(String path);

	int getInt(String path);

	void addSubtreeListener(String subtreeRootNodePath, ZookeeperNodeListener listener);

	List<String> getNodeChildNames(String path);

	void addNodeChildrenListener(String path, ZookeeperNodeListener listener);

	void executeTransaction(ZookeeperTransaction transaction);

	/**
	 * 修改服务地址
	 * @param connectString
	 * @throws Exception
	 */
	void updateServerList(String connectString) throws Exception;

	/**
	 * 设置命名空间
	 * @param newNamespace
	 * @throws Exception
	 */
	void setNamespace(String newNamespace) throws Exception;

	/**
	 * 获取子节点
	 * @param _path
	 * @return
	 * @throws Exception
	 */
	List<String> getChildren(String _path) throws Exception;

	/**
	 * 获取跟路径
	 * @return
	 */
	String getRoot();

	/**
	 * 获取实际路径
	 * @param context
	 * @return
	 */
	String getRealPath(String context);

	/**
	 * 确保节点存在并设置数据
	 * @param _path
	 * @param data
	 */
	void ensureNode(String _path, String data);

	/**
	 * 删除节点
	 * @param _path
	 * @param check
	 */
	void deleteNode(String _path, boolean check);

	/**
	 * 将key转换为路径
	 * @param nodeKey
	 * @return
	 */
	String keyToPath(String nodeKey);

	/**
	 * 将路径转换为key
	 * @param nodePath
	 * @return
	 */
	String pathToKey(String nodePath);

	/**
	 * 通过key获取数据
	 * @param key
	 * @return
	 */
	String getDataByNodeKey(String key);

	/**
	 * 通过key设置数据
	 * @param key
	 * @param data
	 */
	void setDataByNodeKey(String key, String data);

	/**
	 * 同步数据到zookeeper
	 * @param map
	 * @throws Exception
	 */
	void synchDataByNodeKey(Map<String, String> map) throws Exception;

	String pathToKey(String root, String nodePath);
}
